<template>
  <div class="app-container">
    <el-form ref="queryForm" inline :model="queryParams">
      <el-form-item prop="dstIp">
        <el-input
          v-model="queryParams.dstIp"
          clearable
          placeholder="搜索目的IP"
        />
      </el-form-item>
      <el-form-item prop="dstPort">
        <el-input
          v-model="queryParams.dstPort"
          clearable
          placeholder="搜索目的端口"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 查询 </el-button>
        <el-button type="primary" @click="resetQuery"> 重置 </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="pageList"
      fit
      border
      stripe
      height="615px"
    >
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="left"
      ></el-table-column>
      <el-table-column prop="dstIp" show-overflow-tooltip align="left">
        <template slot="header">
          <el-tooltip
            effect="dark"
            content="请根据实际场景设置授权对象的CIDR或者设置单个IP"
            placement="top"
          >
            <span>
              <span style="padding-right: 4px">目的IP</span>
              <i class="el-icon-warning-outline"></i>
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="dstPort" show-overflow-tooltip align="left">
        <template slot="header">
          <el-tooltip
            effect="dark"
            content='取值范围从1到65535；设置格式例如“1/200”、“80/80”，代表不限制端口。支持输入多个端口范围，以","隔开'
            placement="top"
          >
            <span>
              <span style="padding-right: 4px">目的端口</span>
              <i class="el-icon-warning-outline"></i>
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        label="启动状态"
        prop="status"
        show-overflow-tooltip
        align="left"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">启用</el-tag>
          <el-tag v-if="scope.row.status == 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['/eva/v1/rule/update']"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['/eva/v1/rule/delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog
      append-to-body
      center
      width="800px"
      :visible.sync="visible"
      :title="title"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="目的IP" prop="dstIp">
          <el-input v-model="form.dstIp" clearable placeholder="填写目的IP" />
        </el-form-item>
        <el-form-item label="目的端口" prop="dstPort">
          <el-input
            v-model="form.dstPort"
            clearable
            placeholder="填写目的端口"
          />
        </el-form-item>
        <el-form-item label="启动状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRuleList, createRule, updateRule, deleteRule } from "@/api/rule";

import { cloneDeep } from "lodash-es";

export default {
  name: "rulesinfo",
  data() {
    return {
      queryParams: {
        page: 1,
        size: 10,
        appId: this.$route.query.appId,
        deviceId: this.$route.query.deviceId,
        userId: this.$route.query.userId,
        dstIp: undefined,
        dstPort: undefined,
      },
      loading: false,
      pageList: [],
      total: 0,
      visible: false,
      title: "新增",
      form: {},
      rules: {
        dstIp: { required: true, message: "请填写", trigger: "blur" },
        dstPort: { required: true, message: "请填写", trigger: "blur" },
        status: { required: true, message: "请选择", trigger: "change" },
      },
      btnLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    handleAdd() {
      if (!this.queryParams.appId && !this.queryParams.deviceId && !this.queryParams.userIdId) {
        this.$message({
          type: "warning",
          message: "关键参数为空",
        });
        return;
      }

      this.form = {};
      this.resetForm("form");
      this.$set(this.form, "status", 1);
      this.title = "新增";
      this.visible = true;
    },
    handleDelete(row) {
      this.$confirm("确定要删除此规则吗？", "提示", {
        type: "warning",
      }).then(() => {
        deleteRule({ id: row.id }).then((res) => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      });
    },
    cancel() {
      this.form = {};
      this.resetForm("form");
      this.visible = false;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;

          let params = cloneDeep(this.form);
          params.appId = this.queryParams.appId;
          params.deviceId = this.queryParams.deviceId;
          params.userId = this.queryParams.userId;

          if (params.id) {
            updateRule(params)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.getList();
                  this.cancel();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          } else {
            // 新增
            createRule(params)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.getList();
                  this.cancel();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          }
        }
      });
    },
    handleUpdate(row) {
      this.form = { ...row };
      this.title = "编辑";
      this.visible = true;
    },
    getList() {
      if (!this.queryParams.appId && !this.queryParams.deviceId && !this.queryParams.userIdId) {
        this.$message({
          type: "warning",
          message: "关键参数为空",
        });
        return;
      }

      this.loading = true;
      getRuleList(this.queryParams)
        .then((res) => {
          this.pageList = res.data.items || [];
          this.total = res.data.total || 0;
        })
        .finally(() => (this.loading = false));
    },
  },
};
</script>

<style></style>
